

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.utils.config &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.utils.config</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.utils.config</h1><div class="highlight"><pre>
<span></span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">yacs.config</span> <span class="kn">import</span> <span class="n">CfgNode</span>

<span class="kn">from</span> <span class="nn">packnet_sfm.utils.logging</span> <span class="kn">import</span> <span class="n">s3_url</span><span class="p">,</span> <span class="n">prepare_dataset_prefix</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.horovod</span> <span class="kn">import</span> <span class="n">on_rank_0</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.types</span> <span class="kn">import</span> <span class="n">is_cfg</span><span class="p">,</span> <span class="n">is_list</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.misc</span> <span class="kn">import</span> <span class="n">make_list</span>
<span class="kn">from</span> <span class="nn">packnet_sfm.utils.load</span> <span class="kn">import</span> <span class="n">load_class</span><span class="p">,</span> <span class="n">backwards_state_dict</span>


<div class="viewcode-block" id="prep_dataset"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.prep_dataset">[docs]</a><span class="k">def</span> <span class="nf">prep_dataset</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Expand dataset configuration to match split length</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Dataset configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Updated dataset configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If there is no dataset, do nothing</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">config</span>
    <span class="c1"># If cameras is not a double list, make it so</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">config</span><span class="o">.</span><span class="n">cameras</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">is_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">cameras</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
        <span class="n">config</span><span class="o">.</span><span class="n">cameras</span> <span class="o">=</span> <span class="p">[</span><span class="n">config</span><span class="o">.</span><span class="n">cameras</span><span class="p">]</span>
    <span class="c1"># Get maximum length and expand other arguments to the same length</span>
    <span class="n">n</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">cameras</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">depth_type</span><span class="p">))</span>
    <span class="n">config</span><span class="o">.</span><span class="n">dataset</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">dataset</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">split</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">split</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">depth_type</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">depth_type</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">cameras</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">cameras</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="k">if</span> <span class="s1">&#39;repeat&#39;</span> <span class="ow">in</span> <span class="n">config</span><span class="p">:</span>
        <span class="n">config</span><span class="o">.</span><span class="n">repeat</span> <span class="o">=</span> <span class="n">make_list</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">repeat</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
    <span class="c1"># Return updated configuration</span>
    <span class="k">return</span> <span class="n">config</span></div>


<div class="viewcode-block" id="set_name"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.set_name">[docs]</a><span class="k">def</span> <span class="nf">set_name</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Set run name based on available information</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    name : str</span>
<span class="sd">        Updated run name</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If there is a name already, do nothing</span>
    <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Create a name based on available information</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">-</span><span class="si">{}</span><span class="s1">-</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">default</span><span class="p">),</span>
            <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">config</span><span class="p">))[</span><span class="mi">0</span><span class="p">],</span>
            <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y.%m.</span><span class="si">%d</span><span class="s2">-%Hh%Mm%Ss&quot;</span><span class="p">))</span></div>


<div class="viewcode-block" id="set_checkpoint"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.set_checkpoint">[docs]</a><span class="k">def</span> <span class="nf">set_checkpoint</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Set checkpoint information</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Updated model configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If checkpoint is enabled</span>
    <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
        <span class="c1"># Create proper monitor string</span>
        <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">monitor</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">-</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
            <span class="n">prepare_dataset_prefix</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">,</span>
                                   <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">monitor_index</span><span class="p">),</span>
            <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">monitor</span><span class="p">))</span>
        <span class="c1"># Join checkpoint folder with run name</span>
        <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
            <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span><span class="p">,</span> <span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s1">&#39;</span><span class="si">{epoch:02d}</span><span class="s1">_{</span><span class="si">%s</span><span class="s1">:.3f}&#39;</span> <span class="o">%</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">monitor</span><span class="p">)</span>
        <span class="c1"># Set s3 url</span>
        <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">s3_path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">s3_url</span> <span class="o">=</span> <span class="n">s3_url</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># If not saving checkpoint, do not sync to s3</span>
        <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">s3_path</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="k">return</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span></div>


<span class="nd">@on_rank_0</span>
<span class="k">def</span> <span class="nf">prep_logger_and_checkpoint</span><span class="p">(</span><span class="n">model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Use logger and checkpoint information to update configuration</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model : nn.Module</span>
<span class="sd">        Module to update</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Change run name to be the wandb assigned name</span>
    <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">logger</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">dry_run</span><span class="p">:</span>
        <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">run_name</span>
        <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">run_url</span>
        <span class="c1"># If we are saving models we need to update the path</span>
        <span class="k">if</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="c1"># Change checkpoint filepath</span>
            <span class="n">filepath</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">)</span>
            <span class="n">filepath</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">name</span>
            <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="o">=</span> <span class="s1">&#39;/&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">filepath</span><span class="p">)</span>
            <span class="c1"># Change callback dirpath</span>
            <span class="n">dirpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span>
                <span class="n">model</span><span class="o">.</span><span class="n">trainer</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">dirpath</span><span class="p">),</span> <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="n">model</span><span class="o">.</span><span class="n">trainer</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">dirpath</span> <span class="o">=</span> <span class="n">dirpath</span>
            <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">dirpath</span><span class="p">,</span> <span class="n">exist_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">s3_url</span> <span class="o">=</span> <span class="n">s3_url</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="p">)</span>
        <span class="c1"># Log updated configuration</span>
        <span class="n">model</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">log_config</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">config</span><span class="p">)</span>

<div class="viewcode-block" id="get_default_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.get_default_config">[docs]</a><span class="k">def</span> <span class="nf">get_default_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Get default configuration from file&quot;&quot;&quot;</span>
    <span class="n">config</span> <span class="o">=</span> <span class="n">load_class</span><span class="p">(</span><span class="s1">&#39;get_cfg_defaults&#39;</span><span class="p">,</span>
                         <span class="n">paths</span><span class="o">=</span><span class="p">[</span><span class="n">cfg_default</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="s1">&#39;.&#39;</span><span class="p">)],</span>
                         <span class="n">concat</span><span class="o">=</span><span class="kc">False</span><span class="p">)()</span>
    <span class="n">config</span><span class="o">.</span><span class="n">merge_from_list</span><span class="p">([</span><span class="s1">&#39;default&#39;</span><span class="p">,</span> <span class="n">cfg_default</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">config</span></div>

<div class="viewcode-block" id="merge_cfg_file"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.merge_cfg_file">[docs]</a><span class="k">def</span> <span class="nf">merge_cfg_file</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">cfg_file</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Merge configuration file&quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">cfg_file</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">config</span><span class="o">.</span><span class="n">merge_from_file</span><span class="p">(</span><span class="n">cfg_file</span><span class="p">)</span>
        <span class="n">config</span><span class="o">.</span><span class="n">merge_from_list</span><span class="p">([</span><span class="s1">&#39;config&#39;</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">config</span></div>

<div class="viewcode-block" id="merge_cfgs"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.merge_cfgs">[docs]</a><span class="k">def</span> <span class="nf">merge_cfgs</span><span class="p">(</span><span class="n">original</span><span class="p">,</span> <span class="n">override</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Updates CfgNode with information from another one</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    original : CfgNode</span>
<span class="sd">        Original configuration node</span>
<span class="sd">    override : CfgNode</span>
<span class="sd">        Another configuration node used for overriding</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    updated : CfgNode</span>
<span class="sd">        Updated configuration node</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">original</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">override</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">is_cfg</span><span class="p">(</span><span class="n">value</span><span class="p">):</span> <span class="c1"># If it&#39;s a configuration node, recursion</span>
                <span class="n">original</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">merge_cfgs</span><span class="p">(</span><span class="n">original</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">override</span><span class="p">[</span><span class="n">key</span><span class="p">])</span>
            <span class="k">else</span><span class="p">:</span> <span class="c1"># Otherwise, simply update key</span>
                <span class="n">original</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">override</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">original</span></div>

<div class="viewcode-block" id="backwards_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.backwards_config">[docs]</a><span class="k">def</span> <span class="nf">backwards_config</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Add or update configuration for backwards compatibility</span>
<span class="sd">    (no need for it right now, pretrained models are up-to-date with configuration files).</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Updated model configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Return updated configuration</span>
    <span class="k">return</span> <span class="n">config</span></div>

<div class="viewcode-block" id="parse_train_file"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.parse_train_file">[docs]</a><span class="k">def</span> <span class="nf">parse_train_file</span><span class="p">(</span><span class="n">file</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parse file for training</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    file : str</span>
<span class="sd">        File, can be either a</span>
<span class="sd">        **.yaml** for a yacs configuration file or a</span>
<span class="sd">        **.ckpt** for a pre-trained checkpoint file</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Parsed model configuration</span>
<span class="sd">    ckpt : str</span>
<span class="sd">        Parsed checkpoint file</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If it&#39;s a .yaml configuration file</span>
    <span class="k">if</span> <span class="n">file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;yaml&#39;</span><span class="p">):</span>
        <span class="n">cfg_default</span> <span class="o">=</span> <span class="s1">&#39;configs/default_config&#39;</span>
        <span class="k">return</span> <span class="n">parse_train_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">,</span> <span class="n">file</span><span class="p">),</span> <span class="kc">None</span>
    <span class="c1"># If it&#39;s a .ckpt checkpoint file</span>
    <span class="k">elif</span> <span class="n">file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;ckpt&#39;</span><span class="p">):</span>
        <span class="n">checkpoint</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="s1">&#39;cpu&#39;</span><span class="p">)</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">checkpoint</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;config&#39;</span><span class="p">)</span>
        <span class="n">checkpoint</span><span class="p">[</span><span class="s1">&#39;file&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">file</span>
        <span class="k">return</span> <span class="n">config</span><span class="p">,</span> <span class="n">checkpoint</span>
    <span class="c1"># We have a problem</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;You need to provide a .yaml or .ckpt to train&#39;</span><span class="p">)</span></div>


<div class="viewcode-block" id="parse_train_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.parse_train_config">[docs]</a><span class="k">def</span> <span class="nf">parse_train_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parse model configuration for training</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    cfg_default : str</span>
<span class="sd">        Default **.py** configuration file</span>
<span class="sd">    cfg_file : str</span>
<span class="sd">        Configuration **.yaml** file to override the default parameters</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Parsed model configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Loads default configuration</span>
    <span class="n">config</span> <span class="o">=</span> <span class="n">get_default_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">)</span>
    <span class="c1"># Merge configuration file</span>
    <span class="n">config</span> <span class="o">=</span> <span class="n">merge_cfg_file</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">)</span>
    <span class="c1"># Return prepared configuration</span>
    <span class="k">return</span> <span class="n">prepare_train_config</span><span class="p">(</span><span class="n">config</span><span class="p">)</span></div>


<div class="viewcode-block" id="prepare_train_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.prepare_train_config">[docs]</a><span class="k">def</span> <span class="nf">prepare_train_config</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Prepare model configuration for training</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Prepared model configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If arguments have already been prepared, don&#39;t prepare</span>
    <span class="k">if</span> <span class="n">config</span><span class="o">.</span><span class="n">prepared</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">config</span>

    <span class="c1"># Asserts</span>
    <span class="k">assert</span> <span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">dry_run</span> <span class="ow">or</span> <span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">entity</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> \
        <span class="s1">&#39;You need a wandb entity&#39;</span>
    <span class="k">assert</span> <span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">dry_run</span> <span class="ow">or</span> <span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">project</span> <span class="ow">is</span> <span class="ow">not</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> \
        <span class="s1">&#39;You need a wandb project&#39;</span>
    <span class="k">assert</span> <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="ow">is</span> <span class="s1">&#39;&#39;</span> <span class="ow">or</span> \
           <span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">monitor_index</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="o">.</span><span class="n">split</span><span class="p">)),</span> \
        <span class="s1">&#39;You need to monitor a valid dataset&#39;</span>

    <span class="c1"># Prepare datasets</span>
    <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span> <span class="o">=</span> <span class="n">prep_dataset</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span> <span class="o">=</span> <span class="n">prep_dataset</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span> <span class="o">=</span> <span class="n">prep_dataset</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">)</span>
    <span class="c1"># Set name and checkpoint</span>
    <span class="n">config</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">set_name</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
    <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span> <span class="o">=</span> <span class="n">set_checkpoint</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
    <span class="c1"># Return configuration</span>
    <span class="k">return</span> <span class="n">config</span></div>


<div class="viewcode-block" id="parse_test_file"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.parse_test_file">[docs]</a><span class="k">def</span> <span class="nf">parse_test_file</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">,</span> <span class="n">cfg_file</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parse model configuration for testing</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    ckpt_file : str</span>
<span class="sd">        Checkpoint file, with pretrained model</span>
<span class="sd">    cfg_file :</span>
<span class="sd">        Configuration file, to update pretrained model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Parsed model configuration</span>
<span class="sd">    state_dict : dict</span>
<span class="sd">        Model state dict with pretrained weights</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="n">ckpt_file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.ckpt&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">ckpt_file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.pth.tar&#39;</span><span class="p">),</span> \
        <span class="s1">&#39;You need to provide a .ckpt or .pth.tar file for checkpoint, not </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">)</span>
    <span class="k">assert</span> <span class="n">cfg_file</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">cfg_file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;yaml&#39;</span><span class="p">),</span> \
        <span class="s1">&#39;You need to provide a .yaml file for configuration, not </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">cfg_file</span><span class="p">)</span>
    <span class="n">cfg_default</span> <span class="o">=</span> <span class="s1">&#39;configs/default_config&#39;</span>
    <span class="k">return</span> <span class="n">parse_test_config</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">,</span> <span class="n">cfg_default</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">)</span></div>

<div class="viewcode-block" id="parse_test_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.parse_test_config">[docs]</a><span class="k">def</span> <span class="nf">parse_test_config</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">,</span> <span class="n">cfg_default</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Parse model configuration for testing</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    ckpt_file : str</span>
<span class="sd">        Checkpoint file, with pretrained model</span>
<span class="sd">    cfg_default : str</span>
<span class="sd">        Default configuration file, with default values</span>
<span class="sd">    cfg_file : str</span>
<span class="sd">        Configuration file with updated information</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Parsed model configuration</span>
<span class="sd">    state_dict : dict</span>
<span class="sd">        Model state dict with pretrained weights</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">ckpt_file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.ckpt&#39;</span><span class="p">):</span>
        <span class="c1"># Load checkpoint</span>
        <span class="n">ckpt</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="s1">&#39;cpu&#39;</span><span class="p">)</span>
        <span class="c1"># Get base configuration</span>
        <span class="n">config_default</span> <span class="o">=</span> <span class="n">get_default_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">)</span>
        <span class="c1"># Extract configuration and model state</span>
        <span class="n">config_model</span><span class="p">,</span> <span class="n">state_dict</span> <span class="o">=</span> <span class="n">ckpt</span><span class="p">[</span><span class="s1">&#39;config&#39;</span><span class="p">],</span> <span class="n">ckpt</span><span class="p">[</span><span class="s1">&#39;state_dict&#39;</span><span class="p">]</span>
        <span class="c1"># Override default configuration with model configuration</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">merge_cfgs</span><span class="p">(</span><span class="n">config_default</span><span class="p">,</span> <span class="n">config_model</span><span class="p">)</span>
        <span class="c1"># Update configuration for backwards compatibility</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">backwards_config</span><span class="p">(</span><span class="n">config</span><span class="p">)</span>
        <span class="c1"># If another config file is provided, use it</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">merge_cfg_file</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">)</span>
    <span class="c1"># Backwards compatibility with older models</span>
    <span class="k">elif</span> <span class="n">ckpt_file</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;.pth.tar&#39;</span><span class="p">):</span>
        <span class="c1"># Load model state and update it for backwards compatibility</span>
        <span class="n">state_dict</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">,</span> <span class="n">map_location</span><span class="o">=</span><span class="s1">&#39;cpu&#39;</span><span class="p">)[</span><span class="s1">&#39;state_dict&#39;</span><span class="p">]</span>
        <span class="n">state_dict</span> <span class="o">=</span> <span class="n">backwards_state_dict</span><span class="p">(</span><span class="n">state_dict</span><span class="p">)</span>
        <span class="c1"># Get default configuration</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">get_default_config</span><span class="p">(</span><span class="n">cfg_default</span><span class="p">)</span>
        <span class="c1"># If config file is present, update configuration</span>
        <span class="n">config</span> <span class="o">=</span> <span class="n">merge_cfg_file</span><span class="p">(</span><span class="n">config</span><span class="p">,</span> <span class="n">cfg_file</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown checkpoint </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">ckpt_file</span><span class="p">))</span>
    <span class="c1"># Set pretrained model name</span>
    <span class="n">config</span><span class="o">.</span><span class="n">save</span><span class="o">.</span><span class="n">pretrained</span> <span class="o">=</span> <span class="n">ckpt_file</span>
    <span class="c1"># Return prepared configuration and model state</span>
    <span class="k">return</span> <span class="n">prepare_test_config</span><span class="p">(</span><span class="n">config</span><span class="p">),</span> <span class="n">state_dict</span></div>


<div class="viewcode-block" id="prepare_test_config"><a class="viewcode-back" href="../../../utils/utils.config.html#packnet_sfm.utils.config.prepare_test_config">[docs]</a><span class="k">def</span> <span class="nf">prepare_test_config</span><span class="p">(</span><span class="n">config</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Prepare model configuration for testing</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Model configuration</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    config : CfgNode</span>
<span class="sd">        Prepared model configuration</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Remove train and validation datasets</span>
    <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">train</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">validation</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span> <span class="o">=</span> <span class="n">prep_dataset</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">test</span><span class="p">)</span>
    <span class="c1"># Don&#39;t save models or log to wandb</span>
    <span class="n">config</span><span class="o">.</span><span class="n">wandb</span><span class="o">.</span><span class="n">dry_run</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">config</span><span class="o">.</span><span class="n">checkpoint</span><span class="o">.</span><span class="n">filepath</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
    <span class="c1"># Return updated configuration</span>
    <span class="k">return</span> <span class="n">config</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>